package com.polaris.live.ui.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.adapter.live.LiveConsumeAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.DebounceHelper
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.LiveConst.LivePage
import com.polaris.live.databinding.WidgetRoomHeaderBinding
import com.polaris.live.im.sfs.bean.live.LiveRoomInfo
import com.polaris.live.interface_bag.PAGViewListener
import com.polaris.live.resp.back_resp.LiveConsumeUserResp
import com.polaris.live.resp.back_resp.LiveConsumptionUserVo
import com.polaris.live.resp.back_resp.UserRoomThemeResp
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.transformRounded
import com.polaris.live.widget.pag.AddFollowView.Companion.LIVE_ADD_FOLLOWE
import org.libpag.PAGScaleMode
import org.libpag.PAGView

/**
 * RoomHeaderView 房间头部
 *
 * @author Created by 天晴 on 2024/9/21 14:41
 */
class RoomHeaderView(context: Context, attributeSet: AttributeSet) :
    LinearLayout(context, attributeSet) {

    val mBinding = WidgetRoomHeaderBinding.inflate(LayoutInflater.from(context), this)

    //顶部消费列表
    private val mConsumeAdapter by lazy { LiveConsumeAdapter() }

    init {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
    }

    fun initData(@LivePage page: Int, variable: LiveRoomInfo, userCount: Int) {
        mBinding.apply {
            if (rvRecycler.layoutManager == null) {
                mBinding.rvRecycler.apply {
                    layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                    adapter = mConsumeAdapter
                }
            }
            tvRoomName.text = variable.roomName ?: variable.nickname
            pagView.setPathType(LIVE_ADD_FOLLOWE)

            roomId.text = "ID:${variable.roomNumber}"
            Glide.with(context)
                .load(ImageUtils.imgPath(variable.roomCover))
                .transformRounded(6.autoDp)
                .into(roomImg)
            roomFrame.setRoomFrame(variable.roomFrame)

        }
        if (page == LiveConst.Page.LIVE_ANCHOR || page == LiveConst.Page.LIVE_SPECTATOR) {
            updateUserCount((userCount - 1).toLong())
        }
    }

    /**
     * 判断是否关注过
     */
    fun setUserFollower(isFollow: Boolean?) {
        if (isFollow != null) {
            if (isFollow) {
                mBinding.pagView.visibility = View.GONE
            } else {
                mBinding.pagView.visibility = View.VISIBLE
            }
        }
    }

    fun updateConsumeList(data: LiveConsumeUserResp) {
        if (mConsumeAdapter.data != data.users) {
            mConsumeAdapter.setList(data.users)
        }
        updateUserCount(data.total)
    }

    /**
     * 更新直播间人数，只有观众
     */
    fun updateUserCount(userCount: Long) {
        mBinding.tvUserCount.apply {
            if (visibility != VISIBLE) {
                visibility = VISIBLE
            }
            text = userCount.formatNumberWithK()
        }
    }

    fun updateRoomName(name: String) {
        mBinding.tvRoomName.text = name
    }

    fun updateFollowStatus(isFollow: Boolean) {
        if (isFollow) {
            mBinding.pagView.apply {
                setRepeatCount(1)
                addListener(object : PAGViewListener {
                    override fun onAnimationEnd(p0: PAGView?) {
                        super.onAnimationEnd(p0)
                        mBinding.pagView.visibility = View.GONE
                    }
                })
                setScaleMode(PAGScaleMode.Zoom)
                play()
            }

        } else {
            mBinding.pagView.visibility = View.VISIBLE
        }
    }

    fun reset() {
        mConsumeAdapter.data.clear()
        mConsumeAdapter.notifyDataSetChanged()
        mBinding.tvUserCount.text = ""
        mBinding.tvUserCount.visibility = GONE
    }

    fun setOnAvatarClientListener(listener: OnClickListener) {
        mBinding.roomImg.setOnSingleClickListener {
            listener.onClick(it)
        }
    }

    fun setOnFollowerClickListener(listener: OnClickListener) {
        mBinding.pagView.setOnSingleClickListener {
            listener.onClick(it)
        }
    }

    fun setOnConsumeItemClickListener(listener: (Int, LiveConsumptionUserVo) -> Unit) {
        mConsumeAdapter.setOnItemClickListener { _, _, position ->
            DebounceHelper.singleClick {
                val item = mConsumeAdapter.getItem(position)
                listener(position, item)
            }
        }
    }

    //点击在线人数
    fun setOnOnlineClickListener(listener: () -> Unit) {
        mBinding.tvUserCount.setOnSingleClickListener {
            listener.invoke()
        }
    }

    //房间皮肤设置
    fun setTheme(resp: UserRoomThemeResp? = null) {
        if (resp != null) {
            Glide.with(context).load(ImageUtils.imgPath(resp.prefix + resp.frame)).into(mBinding.ivHeadBack)
        } else {
            mBinding.ivHeadBack.setImageResource(R.drawable.party_room_head_back)
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        parent.requestDisallowInterceptTouchEvent(true)
        return super.dispatchTouchEvent(ev)
    }
}